<?php
namespace Pc\Controller;
use Think\Controller;
/**
 *CheckController.class.php 审核数据展示页控制器
 */
class CheckController extends CommonController{
    /* 属性声明 */
    //原始顶级子集文件
    private $item_file = "./Org/Data/init_dic.json";


    /* 方法声明 */
    public function __construct() {
        parent::__construct();
    }

    /*
     * 历史记录
     */
    public function history(){
        $uuid = I('get.uuid');
        $token = I('get.token');
        //先从缓存取数据
        $info = S('history_'.$uuid);
//        if($info){
//            $info = unserialize($info);
//        }else{
            $this -> redis -> select(11);
            //获取当前员工的所有修改流程ID
            if(!$awsids = $this->redis->hkeys('change_'.$uuid)){
                $this->error('您没有历史修改记录!');
            }
            //dump($awsids);exit;
            //获取当前员工审批结束的流程id
            $this->redis->select(12);
            $sp_js_info = unserialize($this->redis->get('sp_js_'.$uuid));

            $info = [];
            foreach($awsids as $k=>$v){
                //获取当前流程的修改数据
                $this -> redis -> select(11);
                $userinfo = unserialize($this->redis->hget('change_'.$uuid,$v));
                //获取当前流程的审批结果
                $this -> redis -> select(12);
                $sp_info = unserialize($this->redis->get('sp_'.$v));

                $time = $userinfo['time'];
                //整理员工基本信息数据
                $base_info = [];
                if($userinfo['information']['BO_CASC_HR_YGRS']){
                    $base_info = $this->getBaseInfo($userinfo['information'],$sp_info);

                }
                //整理扩展子集信息
                $zj_info = $this->getZjInfo($userinfo['information'],$sp_info);

                //获取该流程的审批标志
                $sp_bz = in_array($v,$sp_js_info) ? true : false;
                $info[] = [
                    'time'      => $time,
                    'sp_bz'     => $sp_bz,
                    'base_info' => $base_info,
                    'zj_info'   => $zj_info
                ];
            }
        //array_multisort(array_column($info,'time'),SORT_DESC,$info);
        //dump($info);exit;
            //S('history_'.$uuid,serialize($info),7200);
        //}

        //dump($info);exit;
        /***********************按筛选条件整理数据************************/
        if($info = $this->search($info)){
            //dump($data);exit;
            /*******************按照流程提交时间倒序排序************************/
            array_multisort(array_column($info,'time'),SORT_DESC,$info);
            //dump($info);exit;
            /**************************计算分页数据***********************/
            $page = I('get.page')?I('get.page'):1;
            $count = count($info);//总条数
            $offset = 10;//每页显示的条数
            $ys = $count/$offset;  //总页数
            if(!is_int($ys)){
                $ys = (int)$ys+1;
            }
            $page_info = [];
            foreach($info as $k=>$v){
                if($page == 1){
                    if($k < $offset){
                        $page_info[] = $v;
                    }
                }else{
                    $gt = ($page-1)*$offset-1;
                    $lt = $page*$offset;
                    if($k > $gt && $k < $lt){
                        $page_info[] = $v;
                    }
                }
            }
            //dump($page_info);exit;
            $this->assign(array(
                'info'  => $page_info,
                'count' => $ys,
            ));
        }
        //获取所有子集名
        $menus = $this->getMenuList();
        //dump($menus);exit;
        $this->assign(array(
            'uuid'  => $uuid,
            'menus' => $menus,
            'token' => $token
        ));
        $this->display();
    }

    //按条件检索
    private function search($info){
        //dump($info);exit;
        $sp_type = I('get.sp_type');
        $menu = I('get.menu');
        $ks_sj = I('get.ks_sj');
        $js_sj = I('get.js_sj');
        $sx_info = [];
        if($sp_type || $menu || $ks_sj){
            if($sp_type && $menu && $ks_sj){
                $sp_sx_info = $this->get_sp_type_info($sp_type,$info);
                $menu_sx_info = $this->get_menu_info($menu,$info);
                $sj_sx_info = $this->get_sj_info($ks_sj,$js_sj,$info);
                if(!$sp_sx_info || !$menu_sx_info || !$sj_sx_info){
                    $sx_info = [];
                }else{
                    $sp_key = array_keys($sp_sx_info);
                    $menu_key = array_keys($menu_sx_info);
                    $sj_key = array_keys($sj_sx_info);
                    $keys = array_intersect($sp_key,$menu_key,$sj_key);
                    //dump($keys);exit;
                    if(!$keys){
                        $sx_info = [];
                    }else{
                        foreach($keys as $k=>$v){
                            //这里返回的是子集名称的数据
                            $sx_info[$v] = $menu_sx_info[$v];
                        }
                    }
                }
            }elseif($sp_type && $menu){
                $sp_sx_info = $this->get_sp_type_info($sp_type,$info);
                $menu_sx_info = $this->get_menu_info($menu,$info);
                if(!$sp_sx_info || !$menu_sx_info ){
                    $sx_info = [];
                }else{
                    $sp_key = array_keys($sp_sx_info);
                    $menu_key = array_keys($menu_sx_info);
                    $keys = array_intersect($sp_key,$menu_key);
                    //dump($keys);exit;
                    if(!$keys){
                        $sx_info = [];
                    }else{
                        foreach($keys as $k=>$v){
                            //这里返回的是子集名称的数据
                            $sx_info[$v] = $menu_sx_info[$v];
                        }
                    }
                }
            }elseif($sp_type && $ks_sj){
                $sp_sx_info = $this->get_sp_type_info($sp_type,$info);
                $sj_sx_info = $this->get_sj_info($ks_sj,$js_sj,$info);
                if(!$sp_sx_info || !$sj_sx_info){
                    $sx_info = [];
                }else{
                    $sp_key = array_keys($sp_sx_info);
                    $sj_key = array_keys($sj_sx_info);
                    $keys = array_intersect($sp_key,$sj_key);
                    //dump($keys);exit;
                    if(!$keys){
                        $sx_info = [];
                    }else{
                        foreach($keys as $k=>$v){
                            $sx_info[$v] = $info[$v];
                        }
                    }
                }
            }elseif($menu && $ks_sj){
                $menu_sx_info = $this->get_menu_info($menu,$info);
                $sj_sx_info = $this->get_sj_info($ks_sj,$js_sj,$info);
                if(!$menu_sx_info || !$sj_sx_info){
                    $sx_info = [];
                }else{
                    $menu_key = array_keys($menu_sx_info);
                    $sj_key = array_keys($sj_sx_info);
                    $keys = array_intersect($menu_key,$sj_key);
                    //dump($keys);exit;
                    if(!$keys){
                        $sx_info = [];
                    }else{
                        foreach($keys as $k=>$v) {
                            //这里返回的是子集名称的数据
                            $sx_info[$v] = $menu_sx_info[$v];
                        }
                    }
                }
            }elseif($sp_type){
                $sx_info = $this->get_sp_type_info($sp_type,$info);
            }elseif($menu){
                $sx_info = $this->get_menu_info($menu,$info);
            }elseif($ks_sj){
                $sx_info = $this->get_sj_info($ks_sj,$js_sj,$info);
            }
        }else{
            $sx_info = $info;
        }
        //dump($sx_info);exit;
        return $sx_info;
    }

    //获取根据审批状态筛选的数据
    private function get_sp_type_info($sp_type,$info){
        $sp_type_info = [];
        if($sp_type == 1){
            foreach($info as $k=>$v){
                if(!$v['sp_bz']){
                    $sp_type_info[$k] = $v;
                }
            }
        }elseif($sp_type == 2){
            foreach($info as $k=>$v){
                if($v['sp_bz']){
                    $sp_type_info[$k] = $v;
                }
            }
        }
        return $sp_type_info;
    }
    //获取根据子集名筛选的数据
    private function get_menu_info($menu,$info){
        $menu_info = [];
        if($menu == 'BO_CASC_HR_YGRS'){
            foreach($info as $k=>$v){
                if($v['base_info']){
                    $menu_info[$k]['time'] = $v['time'];
                    $menu_info[$k]['sp_bz'] = $v['sp_bz'];
                    $menu_info[$k]['base_info'] = $v['base_info'];
                }
            }
        }else{
            foreach($info as $k=>$v){
                foreach($v['zj_info'] as $h=>$j){
                    if($menu == $h){
                        $menu_info[$k]['time'] = $v['time'];
                        $menu_info[$k]['sp_bz'] = $v['sp_bz'];
                        $menu_info[$k]['zj_info'][$h] = $j;
                    }
                }
            }
        }
        return $menu_info;
    }
    //获取根据时间筛选的数据
    private function get_sj_info($ks_sj,$js_sj,$info){
        $sj_info = [];
        $ks_sj = strtotime($ks_sj);
        $js_sj = strtotime($js_sj);
        foreach($info as $k=>$v){
            if($v['time'] >= $ks_sj && $v['time'] <= $js_sj){
                $sj_info[$k] = $v;
            }
        }
        return $sj_info;
    }

    /*
    * 预览修改数据
    */
    public function preview(){
        $uuid = I('get.uuid');
        $token = I('get.token');
        //获取redis员工数据
        $this -> redis -> select(10);
        $userinfo = $this -> redis -> hget('user_list',$uuid);
        if(!$userinfo){
            $this -> error('未知成员uuid数据!');
        }
        $userinfo = json_decode($userinfo,true);
        //1.获取成员更改的数据
        $data = A('Update') -> checkInfo($userinfo,$uuid);
        //dump($data);exit;
        $temp = $data['temp'];
        $items = $data['items'];
        $userlist = $temp['information'];
        //dump($userlist);exit;

        //4.如果员工入司基本信息存在,整理员工入司基本信息
        $base_info = [];
        if(in_array('BO_CASC_HR_YGRS',$items)){
            $base_info = $this->getBaseInfo($userlist,$sp_info=null);
        }
        //dump($base_info);exit;

        //5.整理修改的子集信息(过滤和取出字典值)
        $zj_info = $this->getZjInfo($userlist,$sp_info=null);
        //dump($zj_info);exit;

        $this->assign(array(
            'user_name'  => $temp['user_name'],
            'uuid'       => $temp['uuid'],
            'bumen_name' => $temp['bumen_name'],
            'zjlx_name'  => $temp['zjlx_name'],
            'zjhm_name'  => $temp['zjhm_name'],
            'base_info'  => $base_info,//员工基本信息
            'zj_info'    => $zj_info,  //子集信息
            'token'      => $token
        ));

        $this->display();
    }

    /*
     * 审核修改数据  员工基本信息按字段审批
     */
    public function index(){
        $data = I('get.');
        $uuid = $data['uuid'];  //被审核的员工编码
        $awsid = $data['awsid'];  //流程ID
        $jd_bs = (int)$data['no'];  //节点号
        $status = $data['status'];  //单据状态
        $token = $data['token'];  //token

        if(!$uuid || !$awsid || !$jd_bs || !$status || !$token){
            $this->error('参数缺失!');
            exit;
        }
        //验证token
//        $arr = A('Aws')->check_token('/Pc/Check/index',$token);
//        if(!$arr['type']){
//            $this->error($arr['msg']);
//            exit;
//        }
        //1.取出该流程保存的数据
        $this -> redis -> select(11);
        if(!$data= $this->redis->hget('change_'.$uuid,$awsid)){
            $this->error('当前员工的修改数据不存在!');
        }
        $data = unserialize($data);
        $userlist = $data['information'];
        //dump($userlist);

        //3.取出审批信息
        $this->redis->select(12);
        //dump($sp_info = unserialize($this->redis->get('sp_'.$awsid)));exit;
        if(!$sp_info = unserialize($this->redis->get('sp_'.$awsid))){
            //如果审批数据不存在,设置所有数据默认审批通过
            foreach($userlist as $k=>$v){
                $sp_info[$k]['caption'] = $v['caption'];
                if($k == 'BO_CASC_HR_YGRS'){
                    $arr = [];
                    foreach($v['sublist'] as $a=>$b){
                        $arr[$a] = [
                            'sp_type' => 1
                        ];
                    }
                }else{
                    $arr = [];
                    foreach($v['subvalue'] as $a=>$b){
                        $arr[$a] = [
                            'sp_type' => 1
                        ];
                    }
                }
                $sp_info[$k]['sp_info'] = $arr;
            }
            $this->redis->select(12);
            $this->redis->set('sp_'.$awsid,serialize($sp_info));
        }
        //dump($sp_info);exit;

        //4.如果员工入司基本信息存在,整理员工入司基本信息
        $base_info = [];
        if($userlist['BO_CASC_HR_YGRS']){
            $base_info = $this->getBaseInfo($userlist,$sp_info);
        }
        //dump($base_info);exit;

        //5.整理修改的子集信息(过滤和取出字典值)
        $zj_info = $this->getZjInfo($userlist,$sp_info);
        //dump($zj_info);exit;

        $this->assign(array(
            'awsid'      => $awsid,
            'user_name'  => $data['user_name'],
            'uuid'       => $data['uuid'],
            'bumen_name' => $data['bumen_name'],
            'zjlx_name'  => $data['zjlx_name'],
            'zjhm_name'  => $data['zjhm_name'],
            'base_info'  => $base_info,//员工基本信息
            'zj_info'    => $zj_info,  //子集信息
            'jd_bs'      => $jd_bs , //节点标识
            'status'     => $status  //单据状态
        ));

        $this->display();
    }

    //获取员工的基本信息并过滤,取出字典值
    public function getBaseInfo($userlist,$sp_info){
        $item = $userlist['BO_CASC_HR_YGRS'];
        //dump($item);

        $item_name = $item['sublist'];
        //dump($item_name);exit;
        foreach($item_name as $k=>&$v){
            if($v['type'] == 'DATE'){
                $v['value'] = substr($v['value'],0,10);
                if($v['oldvalue'] != '' ){
                    $v['oldvalue'] = substr($v['oldvalue'],0,10);
                }
            }
            $sp_list = $sp_info['BO_CASC_HR_YGRS']['sp_info'];
            //添加审批状态
            if($sp_info){
                $v['sp_info'] = $sp_list[$k];
            }
        }
        $item['sublist'] = $item_name;

        return $item;
    }

    //整理修改的子集信息(过滤)
    public function getZjInfo($userlist,$sp_info){
        $upd_info = [];//整理后的数据
        foreach($userlist as $k=>$v){
            if($k == 'BO_CASC_HR_YGRS'){
                continue;
            }
            $sp_list = $sp_info[$k]['sp_info'];
            $upd_info[$k]['caption'] = $v['caption'];
            $subvalue = $v['subvalue'];
            $status = $v['status'];
            foreach($subvalue as $h=>&$j){
                if($sp_info){
                    $j['sp_info'] = $sp_list[$h];
                }
                $info = $this->guoLv($j['info'],$k);
                //dump($info);exit;
                foreach($info as $a=>&$b){
                    if($b['type'] == 'DATE' ){
                        if($b['value'] != ''){
                            $b['value'] = substr($b['value'],0,10);
                        }
                        if($b['oldvalue'] != '' ){
                            $b['oldvalue'] = substr($b['oldvalue'],0,10);
                        }
                    }
                    //如果是删除的数据,并且修改过 就把值复原
                    if($j['no_display'] && $b['change_type'] == 1 && $b['type'] != 'FILE'){
                        $b['value'] = $b['oldvalue'];
                    }
                }

                //dump($info);
                $j['info'] = $info;
                if(in_array($h,$status['insert'])){
                    $j['type'] = 'insert';
                }else if(in_array($h,$status['update'])){
                    $j['type'] = 'update';
                }else{
                    $j['type'] = 'delete';
                }
            }
            //dump($subvalue);exit;
            $upd_info[$k]['value'] = $subvalue;

        }
        //dump($upd_info);exit;
        return $upd_info;
    }

    //过滤字段
    private function guoLv($info,$name){
        //dump($name);exit;
        $file = "./Org/Data/UserData/main_conf/{$this->corpid}_main.conf";
        if(file_exists($file)){
            $conf_info = unserialize(file_get_contents($file));
            $conf = $conf_info[$name];

            //0.过滤标准
            $filterlist = $conf['sublist'];
            //dump($filterlist);exit;

            //定义临时数据缓冲区
            $subvalue_buf = [];
            $info_keys = array_keys($info);
            foreach($filterlist as $k2 => $v2){
                if(in_array($v2,$info_keys)){
                    $subvalue_buf[$v2] = $info[$v2];
                }
            }
            $info = $subvalue_buf;

        }
        return $info;
    }

    //获取字典值
    private function getItem($name,$val,$dictionaries){
        $item_data = json_decode(file_get_contents($this->item_file),true)["CASC.HR.DICT.ALL"];
        //dump($item_data);exit;
        $item = [];
        foreach($item_data as $k=>$v){
            if($dictionaries == $k){
                $item = $v['subitem'];
            }
        }
        //dump($item);exit;
        $caption = '';
        foreach($item as $k=>$v){
            if($val === $v['value']){
                $caption = $v['caption'];
            }
        }
        //dump($caption);exit;
        return $caption;


    }

    //设置修改数据的审批状态,默认全部通过
    private function setSpInfo($userlist,$awsid){
        foreach($userlist as $k=>$v){
            $sp_info[$k]['caption'] = $v['caption'];
            if($k == 'BO_CASC_HR_YGRS'){
                $arr = [];
                foreach($v['sublist'] as $a=>$b){
                    $arr[$a] = [
                        'sp_type' => 1
                    ];
                }
            }else{
                $arr = [];
                foreach($v['subvalue'] as $a=>$b){
                    $arr[$a] = [
                        'sp_type' => 1
                    ];
                }
            }
            $sp_info[$k]['sp_info'] = $arr;
        }
        $this->redis->select(12);
        $this->redis->set('sp_'.$awsid,serialize($sp_info));
        return $sp_info;
    }

    //ajax处理审批结果
    public function ajax_setApproval(){
        $data = I('post.');
        $awsid = $data['awsid'];
        $item = $data['item'];
        $caption = $data['caption'];
        $type= $data['type'];
        $key = $data['key'];
        $this->redis->select(12);
        $info = unserialize($this->redis->get('sp_'.$awsid));
        //dump($info);exit;
        $itemlist = $info[$item];

        //$itemlist['caption'] = $caption;
        if($type == 1){
            $itemlist['sp_info'][$key] = [
                'sp_type' => 1,
            ];
        }else if($type == 0){
            $itemlist['sp_info'][$key] = [
                'sp_type' => 0,
                'reason' => $data['val']
            ];
        }
        $info[$item] = $itemlist;
        //dump($info);exit;
        $this->redis->select(12);
        if($this->redis->set('sp_'.$awsid,serialize($info))){
            $this->ajaxReturn('ok');
        }else{
            $this->ajaxReturn('fail');
        }
    }

    /*
     * 审核修改数据  员工基本信息整条审批
     */
    public function index2(){
        $uuid = I('get.uuid');  //被审核的员工编码
        $awsid = I('get.awsid');  //流程ID
        $jd_bs = (int)I('get.no');  //节点号
        $status = I('get.status');  //单据状态

        if(!$uuid || !$awsid || !$jd_bs || !$status){
            $this->error('参数缺失!');
        }

        //1.取出该流程保存的数据
        $this -> redis -> select(11);
        if(!$data= $this->redis->hget('change_'.$uuid,$awsid)){
            $this->error('当前员工的修改数据不存在!');
        }
        $data = unserialize($data);
        $userlist = $data['information'];

        //3.取出审批信息
        $this->redis->select(12);
        if(!$sp_info = unserialize($this->redis->get('sp_'.$awsid))){
            //如果审批数据不存在,默认所有数据审批通过
            foreach($userlist as $k=>$v){
                if($k == 'BO_CASC_HR_YGRS'){
                    $sp_info[$k] = [
                        'caption' => $v['caption'],
                        'sp_info' => [
                            0 => [
                                'sp_type' => 1
                            ]
                        ]
                    ];
                }else{
                    $sp_info[$k]['caption'] = $v['caption'];
                    $arr = [];
                    foreach($v['subvalue'] as $a=>$b){
                        $arr[$a] = [
                            'sp_type' => 1
                        ];
                    }
                    $sp_info[$k]['sp_info'] = $arr;
                }
            }
            $this->redis->set('sp_'.$awsid,serialize($sp_info));
        }
        //dump($sp_info);exit;

        //4.如果员工入司基本信息存在,整理员工入司基本信息
        $base_info = [];
        if($userlist['BO_CASC_HR_YGRS']){
            $base_info = $this->getBaseInfo($userlist,$sp_info);
        }
        //dump($base_info);exit;

        //5.整理修改的子集信息(过滤和取出字典值)
        $zj_info = $this->getZjInfo($userlist,$sp_info);
        //dump($zj_info);exit;

        //dump($sp_member_type);exit;
        $this->assign(array(
            'awsid' => $awsid,
            'user_name' => $data['user_name'],
            'uuid' => $data['uuid'],
            'bumen_name' => $data['bumen_name'],
            'zjlx_name' => $data['zjlx_name'],
            'zjhm_name' => $data['zjhm_name'],
            'base_info' => $base_info,//员工基本信息
            'zj_info' => $zj_info,  //子集信息
            'jd_bs' => $jd_bs , //节点标识
            'status' => $status  //单据状态

        ));

        $this->display();
    }
    public function aaa(){
        $arr = [
            'BO_CASC_HR_YGRS' => [
                0 => [
                    'FJ' => [
                        'a.txt' => '/Public/Uploads/xxxxx.txt',
                        'b.txt' => '/Public/Uploads/xxxxx.txt',
                    ]
                ]
            ],
        ];
        $brr = [
            'BO_CASC_HR_XQXQ' => [
                'awsid1.......' => [
                    'HTXY' => [
                        'a.txt' => '/Public/Uploads/xxxxx.txt',
                        'b.txt' => '/Public/Uploads/xxxxx.txt',
                    ],
                    'HTWB' => [
                        'a.txt' => '/Public/Uploads/xxxxx.txt',
                        'b.txt' => '/Public/Uploads/xxxxx.txt',
                    ]
                ]
            ]
        ];
        echo json_encode($arr);
        echo '<hr>';
        echo json_encode($brr);
    }
}